package com.mfcxy.admin.controller;

import java.util.HashMap;
import java.util.Map;

import javax.xml.ws.RequestWrapper;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fasterxml.jackson.annotation.JsonCreator.Mode;
import com.mfcxy.service.IRolePermissionService;
import com.mfcxy.service.IRoleService;
import com.mfcxy.tools.BackJson;

@RequestMapping("/admin/role/")
@Controller
public class RoleController {
	@Autowired
	private IRoleService roleService;
	
	@Autowired
	private IRolePermissionService rp;
	
	@RequestMapping("list")
	public String list(Model model) {
		model.addAttribute("list", roleService.selectList());
		return "admin/role_list";
	}
	
	/**
	 * ajax 獲取角色關聯的權限
	 * @param rid
	 * @return
	 */
	@ResponseBody
	@RequestMapping("getrp/{rid}")
	public BackJson getrp(@PathVariable int rid) {
		BackJson bj = new BackJson();
		bj.setCode(1);
		Map<String, Object> map = new HashMap<>();
		map.put("list", rp.selectRidAndPid(rid));
		bj.setData(map);
		return bj;
	}
	
	/**
	 * 更新权限
	 * @param pids
	 * @param rid
	 * @return
	 */
	@ResponseBody
	@RequestMapping("updatepermissions")
	public BackJson updatepermissions(@RequestParam String pids , @RequestParam int rid) {
		rp.updateRidAndPid(pids, rid);
		BackJson bj = new BackJson();
		bj.setCode(1);
		return bj;
	}
	
	
}
